package LeetCode.leetcode;


/**
 * 搜索旋转排序数组
 * 4 5 6 1 2 3
 * @author: leiwenkang02@meituan.com
 * @date: 2018/10/25
 * @time: 11:17
 */
public class Test33搜索旋转排序数组 {

    public int search(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return -1;
        }

        int start = 0;
        int end = nums.length - 1;

        while (start <= end) {
            int mid = (start + end) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] > nums[start]) {
                if (target >=  nums[start] && target < nums[mid]) {
                    end = mid - 1;
                } else {
                    start = mid + 1;
                }
            } else {
                if (target >  nums[mid] && target <= nums[end]) {
                    start = mid + 1;
                } else {
                    end = mid - 1;
                }
            }
        }
        return -1;

    }
}


